<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 客户信息
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                条件查询<p/>
                        开始时间
                        <el-date-picker
                                class="handle-select mr20 "
                                v-model="query.startDate"
                                type="datetime"
                                value-format="yyyy-MM-dd HH:mm:ss"
                                placeholder="选择日期"
                                :picker-options="pickerOptions0">
                        </el-date-picker>
                        结束时间
                            <el-date-picker
                                    class="handle-select mr20"
                                    v-model="query.endDate"
                                    type="datetime"
                                    value-format="yyyy-MM-dd HH:mm:ss"
                                    placeholder="选择日期"
                                    :picker-options="pickerOptions1">
                            </el-date-picker>
                状态查询
                <el-select v-model="query.status" placeholder="状态查询" class="handle-select mr10">
                    <el-option key="2" label="查询全部" value=""></el-option>
                    <el-option key="1" label="正常状态" value="正常"></el-option>
                    <el-option key="1" label="已冻结" value="冻结"></el-option>
                </el-select>
                <p/>
                <el-row style="margin-top: 10px">
                <el-input v-model="query.name" placeholder="用户名" class="handle-input mr10"></el-input>
                <el-input v-model="query.phone" placeholder="用户手机号" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                </el-row>
                <el-button style="float: right"
                        type="primary"
                        icon="el-icon-delete"
                        class="handle-del mr10"
                        @click="delAllSelection"
                >批量冻结</el-button>
            </div>
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="userid" label="ID" width="55" align="center"></el-table-column>
                <el-table-column prop="nickname" label="用户名"></el-table-column>
                <el-table-column label="生日">
                    <template slot-scope="scope">{{scope.row.birthday}}</template>
                </el-table-column>
                <el-table-column label="头像(查看大图)" align="center">
                    <template slot-scope="scope">
                        <el-image
                            class="table-td-thumb"
                            :src="scope.row.userimg"
                            :preview-src-list="[scope.row.userimg]"
                        ></el-image>
                    </template>
                </el-table-column>
                <el-table-column prop="usersex" label="性别"></el-table-column>
                <el-table-column prop="userphone" label="手机号"></el-table-column>
                <el-table-column prop="state" label="状态" align="center"></el-table-column>

                <el-table-column prop="createdate" label="注册时间"></el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            icon="el-icon-edit"
                            @click="handleEdit(scope.$index, scope.row)"
                        >详情</el-button>
                        <el-button
                                v-if="scope.row.state=='正常'"
                            type="text"
                            icon="el-icon-delete"
                            class="red"
                            @click="handleDelete(scope.$index, scope.row)"
                        >冻结</el-button>
                        <el-button
                                v-else
                                type="text"
                                icon="el-icon-delete"
                                class="green"
                                @click="handleUp(scope.$index, scope.row)"
                        >解冻</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.pageIndex"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 详情弹出框 -->
        <el-dialog title="详情查看" :visible.sync="editVisible" width="80%">
            <el-table
                    :data="form"
                    style="width: 100%">
                <el-table-column
                        prop="userid"
                        label="用户编号"
                        width="80">
                </el-table-column>
                <el-table-column
                        prop="nickname"
                        label="姓名"
                        width="100">
                </el-table-column>
                <el-table-column
                        prop="userphone"
                        label="电话">
                </el-table-column>
                <el-table-column
                        prop="usersex"
                        label="性别">
                </el-table-column>
                <el-table-column
                        prop="birthday"
                        label="生日">
                </el-table-column>
                <el-table-column
                        prop="hobby"
                        label="爱好">
                </el-table-column>
                <el-table-column
                        prop="signature"
                        label="个性签名" width="180">
                </el-table-column>
                <el-table-column
                        prop="createdate"
                        label="注册日期">
                </el-table-column>
                <el-table-column
                        prop="state"
                        label="状态">
                </el-table-column>
            </el-table>
        </el-dialog>
    </div>
</template>

<script>
import { fetchData } from '../../api/index';
export default {
    name: 'Customer',
    data() {
        return {
            query: {
                phone: '',
                name: '',
                pageIndex: 1,
                pageSize: 10,
                startDate:'',
                endDate:'',
                status:''
            },
            users:[],
            cinemas:['中','习'],
            tableData: [],
            multipleSelection: [],
            delList: [],
            editVisible: false,
            pageTotal: 0,
            form: [],
            idx: -1,
            id: -1,
            pickerOptions0: {
                disabledDate:(time)=> {
                    return time.getTime() > Date.now() - 8.64e7 ;
                }
            },
            pickerOptions1: {
                disabledDate:(time)=> {
                    if (this.query.startDate==''){
                        return true
                    }
                    if (time.getTime()<this.query.startDate){
                        return true
                    }
                    return time.getTime() > Date.now()- 8.64e7 ;
                }
            },
        };
    },
    created() {
        this.getData();
    },
    methods: {
        // 获取 easy-mock 的模拟数据
        getData:function() {
            /*fetchData(this.query).then(res => {
                console.log(res);
                this.tableData = res.list;
                this.pageTotal = res.pageTotal || 50;
            });*/

            this.axios.get('/api/user/list',{params:this.query}).then((data)=>{
                // console.log(data.data);
               this.tableData=data.data.list;
                this.pageTotal = data.data.total || 0;
            })
        },
        // 触发搜索按钮
        handleSearch() {
            this.getData();

        },
        // 冻结操作
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要冻结该用户吗？', '提示', {
            })
                .then(() => {
                    // console.log(row.userid)
                    this.axios.get("/api/user/unsubscribe",{params:{
                        userid:row.userid
                        }}).then((data)=>{
                        if (data.data==1){
                            this.$message.success('操作成功');
                            this.getData();
                        }else{
                            this.$message.success('操作失败');
                        }
                    })

                })
        },
        // 解除操作
        handleUp(index, row) {
            // 二次确认解冻
            this.$confirm('确定要解除该用户的冻结吗？', '提示', {
            })
                .then(() => {
                    // console.log(row.userid)
                    this.axios.get("/api/user/subscribe",{params:{
                            userid:row.userid
                        }}).then((data)=>{
                        if (data.data==1){
                            this.$message.success('操作成功');
                            this.getData();
                        }else{
                            this.$message.success('操作失败');
                        }
                    })

                })
        },
        // 多选操作
        handleSelectionChange(val) {

            this.multipleSelection = val;
        },
        //多删除
        delAllSelection() {
            const length = this.multipleSelection.length;
            let str = '';
            this.delList = this.delList.concat(this.multipleSelection);
            for (let i = 0; i < length; i++) {
                str += this.multipleSelection[i].userid + ',';
            }
            this.axios.get("/api/user/unsubscribeByCheck",{params:{
                    userids:str
                }}).then((data)=>{
                    if (data!=0){
                        this.$message.error(`冻结成功`);
                        this.getData()
                    }else {
                        this.$message.error(`冻结失败`);
                    }
            }).catch(()=>{
                this.$message.error(`服务器错误`);
            })
            this.multipleSelection = [];
        },
        // 详情操作
        handleEdit(index, row) {
            this.idx = index;
            this.form[0] = row;
            this.editVisible = true;
        },
        // 保存编辑
        saveEdit() {
            this.editVisible = false;
            this.$message.success(`修改第 ${this.idx + 1} 行成功`);
            this.$set(this.tableData, this.idx, this.form);
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageIndex', val);
            this.getData();
        },
        select(){
            axios.post(url)
        }
    }
};

</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.green {
    color: #85ce61;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
